package edu.ncu.dao;

import edu.ncu.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.*;

public interface UserDao {
    @Select("select * from user;")
    List<User> findAll();

    @Select({"<script> " +
            "select * from user " +
            "where  1=1 " +
            "<if test='account!=null'> and account like #{account}</if> " +
            "<if test='uname!=null'> and uname like #{uname}</if> " +
            "</script>"})
    //模糊查询，传参的时候要加%
    List<User> findByFuzzy(@Param("account") String account, @Param("uname") String uname);

    @Select("select * from user where userId = #{userId};")
    User findByUserId(int userId);

    @Select("select * from user where account = #{account};")
    User findByAccount(String account);
    @Select("select * from user where account like '%${account}%';")
    List<User> findLikeAccount(String account) ;


    @Update("update user set state = #{state} where userId = #{userId};")
    void updateUserState(@Param("state") int state, @Param("userId") int userId);

    @Update("update user set headPhoto = #{headPhoto} where userId = #{userId};")
    void updateUserHead(@Param("headPhoto") String headPhoto, @Param("userId") int userId);

    @Insert("insert into user (account, uname, upwd, age, sex, description, headPhoto, integral, grade, state) " +
            "values(#{account}, #{uname}, #{upwd}, #{age}, #{sex}, #{description}, #{headPhoto}, #{integral}, #{grade}, #{state});")
    void saveUser(User user);

    @Update("update user set description=#{description}, age=#{age}, uname=#{uname}, upwd=#{upwd}, sex=#{sex} " +
            "where userId = #{userId};")
    void updateUser(User user);

}
